package com.hao.flowsvr.service.impl;

import com.hao.flowsvr.constant.ErrorStatusReturn;
import com.hao.flowsvr.mapper.ScheduleConfigMapper;
import com.hao.flowsvr.model.entity.ReturnStatus;
import com.hao.flowsvr.model.entity.ScheduleConfig;
import com.hao.flowsvr.model.enums.ErrorStatus;
import com.hao.flowsvr.model.vo.ConfigReturn;
import com.hao.flowsvr.service.ScheduleConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ScheduleConfigServiceImpl implements ScheduleConfigService {
    @Resource
    private ScheduleConfigMapper scheduleConfigMapper;

    Logger logger = LoggerFactory.getLogger(ScheduleConfigServiceImpl.class);

    @Override
    public <T> ReturnStatus<T> getTaskTypeCfgList() {
        List<ScheduleConfig> taskTypeCfgList;
        try {
            taskTypeCfgList = scheduleConfigMapper.getTaskTypeCfgList();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ErrorStatus.ERR_GET_TASK_CFG_FROM_DB.getMsg());
            return ErrorStatusReturn.ERR_GET_TASK_CFG_FROM_DB;
        }
        ConfigReturn configReturn = new ConfigReturn(taskTypeCfgList);
        return new ReturnStatus(configReturn);
    }

    @Override
    public <T> ReturnStatus<T> save(ScheduleConfig scheduleConfig) {
        long currentTimeMillis = System.currentTimeMillis();
        scheduleConfig.setCreate_time(currentTimeMillis);
        scheduleConfig.setModify_time(currentTimeMillis);
        try {
            scheduleConfigMapper.save(scheduleConfig);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ErrorStatus.ERR_SET_TASK_CFG_FROM_DB.getMsg());
        }
        return ErrorStatusReturn.SUCCESS;
    }
}
